//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "vSRefMultiple.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall TRefMultiple::TRefMultiple(const int &nNbRef,
                                      const int &nLogiciel,
                                      const int &nVersion,
                                      const AnsiString &asRefNom,
                                      const AnsiString &asRefDesignation,
                                      const AnsiString &asRefAdresse,
                                      const int &nCbAttente,
                                      TComponent* Owner)
    : TForm(Owner)
{
    try {
        //Remplissage de la combo attente
        //-------------------------------
        CComboAttente * Attente = NULL;
        Attente = new CComboAttente();
        Attente->Remplir(cbAttente->Items);
        if(Attente) {delete Attente; Attente = NULL;}

        bConsulter->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_CONSULTER");
        lEntete->Caption = PAGE_MULTIPLE;
        //Initialiser les variables
        //-------------------------
        nIndexPanel = 0;
        m_nLogiciel = nLogiciel;
        m_nVersion  = nVersion;
        m_asRefNom   = asRefNom;

        AfficherRefMultiple(nNbRef);
        TZZMySqlQuery * MaRequete = NULL;
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select log_nom,"+       //0
                                                    "ver_nom,"+      //1
                                                    "ref_sous_nom,"+ //2
                                                    "ref_adresse,"+  //3
                                                    "ref_attente "+  //4
                                            "from refer "+
                                                "left outer join logiciels on "+
                                                    "(refer.log_code = logiciels.log_code) "+
                                                "inner join versions on "+
                                                    "(refer.ver_code = versions.ver_code) "+
                                            "where (refer.log_code = "+m_nLogiciel+" and refer.ver_code = "+m_nVersion+") and ref_nom = \'"+asRefNom+"\'";
        MaRequete->Open();
        MaRequete->First();
        eLogiciel->Text    =   asRequete(0);
        eVersion->Text     =   asRequete(1);
        eReference->Text   =   asRefNom;

        if( !asRefAdresse.IsEmpty() ){
            ePageWeb->Text = asRefAdresse;
        }

        if( !asRefDesignation.IsEmpty() ) {
            eDesignation->Text = asRefDesignation;
        }else {
            if(nCbAttente == AUCUNE_SELECTION)
                eDesignation->Color = 0x000000F0;
            else
                cbAttente->ItemIndex = nCbAttente;
        }

        while(!MaRequete->Eof) {
            RenseignerChamp(asRequete(2), asRequete(3), nRequete(4));
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();
        if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//              AFFICHER REFERENCE MULTIPLE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherRefMultiple( const int &nNbreRef )
{
    try {
        TPanel * Panel = NULL;
        for(int i=0; i<(nNbreRef); i++) {
            Panel               = new TPanel(pReste);
            Panel->Name         = (AnsiString)"Panel"+i;
            AfficherZonePanel( Panel );
        }

        //Calcul du Range pour la ScrollBox
        //---------------------------------
        sbAdresseMultiple->VertScrollBar->Range = 25 + (80*(nNbreRef));
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//              STRUCTURE ZONE PANEL
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherZonePanel( TPanel * Panel )
{
    try {
        TEdit       * Designation;
        TEdit       * PageWeb;
        TLabel      * lDesignation;
        TLabel      * lPageWeb;
        TLabel      * lAttente;
        TComboBox   * cbAttente;
        TBitBtn     * bConsulter;

        Panel->Parent       = pReste;
        Panel->Align        = alTop;
        Panel->Width        = 502;
        Panel->Height       = 80;
        Panel->Color        = clMenu;

        cbAttente           = new TComboBox(Panel);     //Composant 0
        PageWeb             = new TEdit(Panel);         //Composant 1
        Designation         = new TEdit(Panel);         //Composant 2
        lDesignation        = new TLabel(Panel);        //Composant 3
        lPageWeb            = new TLabel(Panel);        //Composant 4
        lAttente            = new TLabel(Panel);        //Composant 5
        bConsulter          = new TBitBtn(Panel);       //Composant 6

        //Designation
        //-----------
        Designation->Parent = Panel;
        Designation->Left   = 112;
        Designation->Top    = 4;
        Designation->Width  = 382;
        Designation->Height = 21;
        Designation->Color  = clWhite;
        Designation->Name   = "Designation";
        Designation->Text   = "";

        lDesignation->Parent= Panel;
        lDesignation->Caption = "Désignation";
        lDesignation->Font->Style = TFontStyles()<< fsBold << fsUnderline;
        lDesignation->Left  = 8;
        lDesignation->Top   = 4;

        //Page Web
        //--------
        PageWeb->Parent     = Panel;
        PageWeb->Name       = "PageWeb";
        PageWeb->Left       = 112;
        PageWeb->Top        = 28;
        PageWeb->Width      = 382;
        PageWeb->Height     = 21;
        PageWeb->Color      = clWhite;
        PageWeb->Text       = "";
        PageWeb->ReadOnly   = true;

        lPageWeb->Parent    = Panel;
        lPageWeb->Caption   = "Page Web";
        lPageWeb->Left      = 8;
        lPageWeb->Top       = 28;

        bConsulter->Parent  = Panel;
        bConsulter->Name    = "Consulter";
        bConsulter->Caption = "";
        bConsulter->Left    = 72;
        bConsulter->Top     = 21;
        bConsulter->Height  = 20;
        bConsulter->Width   = 25;
        bConsulter->Glyph->LoadFromResourceName((int)HInstance, "ID_ACTION_CONSULTER");


        //Page d'attente
        //--------------
        cbAttente->Parent     = Panel;
        cbAttente->Name       = "Attente";
        cbAttente->Text       = "";  
        cbAttente->Left       = 112;
        cbAttente->Top        = 52;
        cbAttente->Width      = 382;
        cbAttente->Height     = 21;
        //Remplissage de la combo attente
        //-------------------------------
        CComboAttente * Attente = NULL;
        Attente = new CComboAttente();
        Attente->Remplir(cbAttente->Items);
        if(Attente) {delete Attente; Attente = NULL;}

        lAttente->Parent     = Panel;
        lAttente->Caption    = "Page d'attente";
        lAttente->Left       = 8;
        lAttente->Top        = 52;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//              UTILISATION DES COMPOSANTS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::RenseignerChamp(  const AnsiString &asDesignation,
                                                const AnsiString &asPageWeb,
                                                const int &nCbAttente)
{
    try {
        //Permet de nous informer sur le nom du panel utilisé
        //---------------------------------------------------
        TComponent * pChildPanel = pReste->Components[nIndexPanel];

        //Composant fils du panel
        //-----------------------
        for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {

            TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

            if( dynamic_cast<TEdit*>(pChildFilsPanel)) {
                if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                    if(!asDesignation.IsEmpty()) {
                        ((TEdit*)pChildFilsPanel)->Text = asDesignation;
                    }else{
                        if(asPageWeb != EN_COURS_DE_MISE_A_JOUR)
                            ((TEdit*)pChildFilsPanel)->Color = clRed;
                    }

                }

                if( ((TEdit*)pChildFilsPanel)->Name == "PageWeb") {
                    if(asPageWeb != EN_COURS_DE_MISE_A_JOUR){
                        ((TEdit*)pChildFilsPanel)->Text = asPageWeb;
                    }
                }
            }

            if( dynamic_cast<TButton*>(pChildFilsPanel)) {
                if( ((TButton*)pChildFilsPanel)->Name == "Consulter") {
                    ((TButton*)pChildFilsPanel)->OnClick = AfficherPageWeb;
                }
            }

            if( dynamic_cast<TComboBox*>(pChildFilsPanel)) {
                if( ((TComboBox*)pChildFilsPanel)->Name == "Attente") {
                    if(nCbAttente != AUCUNE_SELECTION)
                        ((TComboBox*)pChildFilsPanel)->ItemIndex = nCbAttente;
                }
            }
        }
        //Prochain panel
        //--------------
        nIndexPanel++;
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bConsulterClick(TObject *Sender)
{
    try {
        ShellExecute(NULL, "Open", ePageWeb->Text.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//          AFFICHER PAGE WEB VENANT DES BOUTONS DYNAMIQUE
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::AfficherPageWeb(TObject *Sender)
{
    try {
        AnsiString asAdresse = "";
        //Recuperation du bouton appuyé
        //-----------------------------
        TButton * MonBouton = dynamic_cast<TButton*>(Sender);
        //Parent du bouton
        //----------------
        TPanel * MonPanel = dynamic_cast<TPanel*>(MonBouton->Parent);

        for(int i=0; i<MonPanel->ComponentCount;i++) {
            TComponent * pFils  = MonPanel->Components[i];
            if( dynamic_cast<TEdit*>(pFils)) {
                if( ((TEdit*)pFils)->Name == "PageWeb") {
                    asAdresse = ((TEdit*)pFils)->Text;
                }
            }
        }
        if(!asAdresse.IsEmpty())
            ShellExecute(NULL, "Open", asAdresse.c_str(), NULL, NULL, SW_SHOWMAXIMIZED);
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//                          VALIDATION
//---------------------------------------------------------------------------
void __fastcall TRefMultiple::bbOkClick(TObject *Sender)
{
    try {
        if( VerifierDesignation() ) {
            //Recuperer toutes les designations
            //---------------------------------
            queue<AnsiString> qDesignation;

            //FIFO temporaire permettant la récupération du ref_code associé à une référence et une adresse
            //---------------------------------------------------------------------------------------------
            queue<AnsiString> qTempPageWeb;

            //Remplissage des FIFO qDesignation et qPageWeb
            //---------------------------------------------
            for(int i=0; i<pReste->ComponentCount; i++) {
                TComponent * pChildPanel = pReste->Components[i];
                for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                    TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];
                    //Composant TComboBox
                    //-------------------
                    if( dynamic_cast<TComboBox*>(pChildFilsPanel)) {
                        if( ((TComboBox*)pChildFilsPanel)->Name == "Attente") {
                            if(((TComboBox*)pChildFilsPanel)->ItemIndex != AUCUNE_SELECTION) {
                                qTempPageWeb.push(((TComboBox*)pChildFilsPanel)->Items->Strings[((TComboBox*)pChildFilsPanel)->ItemIndex]);
                            }
                        }
                    }

                    //Composant TEdit
                    //---------------
                    if( dynamic_cast<TEdit*>(pChildFilsPanel)) {

                        if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                            qDesignation.push(((TEdit*)pChildFilsPanel)->Text);
                        }

                        if( ((TEdit*)pChildFilsPanel)->Name == "PageWeb") {
                            if(!((TEdit*)pChildFilsPanel)->Text.IsEmpty())
                                qTempPageWeb.push(((TEdit*)pChildFilsPanel)->Text);
                        }
                    }
                }
            }

            queue<int> qRefCode;
            //Remplissage de la FIFO qRefCode
            //-------------------------------
            while(!qTempPageWeb.empty()) {
                qRefCode.push( GetRefCode(qTempPageWeb.front()) );
                qTempPageWeb.pop();
            }

            //Insertion dans la base WebHelp
            //------------------------------
            dmWebHelp->tRefer->Active = true;
            dmWebHelp->tRefer->Insert();
            ntRefer(LOG_CODE)       = m_nLogiciel;
            ntRefer(VER_CODE)       = m_nVersion;
            astRefer(REF_NOM)       = m_asRefNom;
            astRefer(REF_SOUS_NOM)  = eDesignation->Text;
            if(!ePageWeb->Text.IsEmpty())
                astRefer(REF_ADRESSE)   = ePageWeb->Text;
            else
                astRefer(REF_ADRESSE) = cbAttente->Items->Strings[cbAttente->ItemIndex];

            dmWebHelp->tRefer->Post();
            dmWebHelp->trWebHelp->Commit();

            //Mise à jour des champ désignation
            //---------------------------------
            dmWebHelp->tRefer->Active = true;
            while(   !qRefCode.empty()
                  && !qDesignation.empty()){

                dmWebHelp->tRefer->First();
                while(!dmWebHelp->tRefer->Eof) {
                    if(ntRefer(REF_CODE) == qRefCode.front()) {
                        dmWebHelp->tRefer->Edit();
                        astRefer(REF_SOUS_NOM)  = qDesignation.front();
                        dmWebHelp->tRefer->Post();
                        dmWebHelp->trWebHelp->Commit();
                    
                        qRefCode.pop();
                        qDesignation.pop();
                        break;
                    }
                    dmWebHelp->tRefer->Next();
                }
            }
            //Validation OK
            //-------------
            ModalResult = mrOk;
        }
        else {
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
    }
}
//---------------------------------------------------------------------------
//                  RECUPERATION DU REF_CODE
//---------------------------------------------------------------------------
int  __fastcall TRefMultiple::GetRefCode( const AnsiString &asPageWeb )
{
    int RefCode = 0;
    try {
        //Recuperer le ref_code permettant d'inserer au bon endroit la désignation
        //------------------------------------------------------------------------
        TZZMySqlQuery * MaRequete = NULL;
        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ref_code "+
                                            "from refer where (log_code="+m_nLogiciel+" and ver_code="+m_nVersion+") "+
                                            "and (ref_nom=\'"+m_asRefNom+"\' and ref_adresse=\'"+asPageWeb+"\')";
        MaRequete->Open();
        MaRequete->First();

        RefCode = nRequete(0);

        MaRequete->Sql->Clear();
        MaRequete->Close();
        if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        RefCode = 0;
    }
    return RefCode;
}
//---------------------------------------------------------------------------
//                  VERIFIER LES DESIGNATIONS NON REMPLIES
//---------------------------------------------------------------------------
bool __fastcall TRefMultiple::VerifierDesignation()
{
    bool bRetour = false;
    try {
        bool bAttente = false;
        queue<bool> qVerif;

        if(!eDesignation->Text.IsEmpty()) {
            eDesignation->Color = clWhite;
            qVerif.push(true);
        }

        else {
            if(cbAttente->ItemIndex == AUCUNE_SELECTION){
                eDesignation->Color = 0x000000F0;
                qVerif.push(false);
            }
        }

        for(int i=0; i<pReste->ComponentCount; i++) {
            TComponent * pChildPanel = pReste->Components[i];
            for(int IndexFilsPanel=0;IndexFilsPanel<((TPanel*)pChildPanel)->ComponentCount;IndexFilsPanel++) {
                TComponent * pChildFilsPanel  = pChildPanel->Components[IndexFilsPanel];

                //Composant TComboBox
                //-------------------
                if( dynamic_cast<TComboBox*>(pChildFilsPanel)) {
                    if( ((TComboBox*)pChildFilsPanel)->Name == "Attente") {
                        if(((TComboBox*)pChildFilsPanel)->ItemIndex != AUCUNE_SELECTION) {
                            qVerif.push(true);
                        }else {
                            bAttente = true;
                        }
                    }
                }

                //Composant TEdit
                //---------------
                if( dynamic_cast<TEdit*>(pChildFilsPanel)) {
                    if( ((TEdit*)pChildFilsPanel)->Name == "Designation") {
                        if( !((TEdit*)pChildFilsPanel)->Text.IsEmpty() ) {
                            ((TEdit*)pChildFilsPanel)->Color = clWhite;
                            qVerif.push(true);
                        }
                        else {
                            if(bAttente)
                                ((TEdit*)pChildFilsPanel)->Color = clRed;
                        }
                    }
                }
            }
        }

        while(!qVerif.empty()) {
            if(!qVerif.front()) {
                bRetour = false;
                break;
            }
            else
                bRetour = true;
           qVerif.pop();
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);
        bRetour = false;
    }
    return bRetour;
}
//---------------------------------------------------------------------------
